//
//  StaticController.swift
//  XPApp
//
//  Created by zhuxietong on 2017/2/11.
//  Copyright © 2017年 zhuxietong. All rights reserved.
//

import Foundation
//
import WebKit


open class StaticController: HTMLController,TagsRenderDelegate{
    
    public var htmlBundle:String
    public var templateName:String
    public var css:[String] = ["app"]
    public var js:[String] = ["ios"]
    
    public var js_config = new_js_config
    public var net = JoTask()
    
    
    required public init(htmlBundle:String = "html",templateName:String = ""){
        let js = new_js_config
        self.templateName = templateName
        self.htmlBundle = htmlBundle
        super.init(render: nil, wk_config: js)
        weak var wself = self
        js.receiveJsAction = {
            (message,controller)in
            if let one = message.body as? Dictionary<String, AnyObject>
            {
                let dict = one.mutable_dictionary
                wself?.observerJSAction(info: dict)
            }
        }        
    }
    
    required public init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
        
    }
    
    required public init(render: HTMLPageRender?, wk_config: WKWebViewConfiguration) {
        fatalError("init(render:wk_config:) has not been implemented")
    }
    
    required public init() {
        let js = WebConfiguration()
        js.initBlock = { (hander,controller) in
            controller.add(hander, name: "__sendtoapp")
            let appUserInfo =
            "function __getClientInfo(){" +
                "    return {'device-type':'iOS'} ;" +
            "}"
            let script11 = WKUserScript(source: appUserInfo, injectionTime: WKUserScriptInjectionTime.atDocumentStart, forMainFrameOnly: true)
            let script12 = WKUserScript(source: appUserInfo, injectionTime: WKUserScriptInjectionTime.atDocumentEnd, forMainFrameOnly: true)
            controller.addUserScript(script11)
            controller.addUserScript(script12)
        }
        
        self.templateName = "templateName"
        self.htmlBundle = "htmlBundle"
        super.init(render: nil, wk_config: js)
        weak var wself = self
        js.receiveJsAction = {
            (message,controller)in
            if let one = message.body as? Dictionary<String, AnyObject>
            {
                let dict = one.mutable_dictionary
                wself?.observerJSAction(info: dict)
            }
        }
        
    }
    
    
    open func observerJSAction(info:NSMutableDictionary) {
        
    }
    
    
    open override func viewDidLoad() {
        self.htmlRender = self.render
        super.viewDidLoad()
        
        //        ctr_style = CtrStyle.default
        self.jo_contentView.backgroundColor = ._background
        view.backgroundColor = ._background
        webView?.isOpaque = false
        webView?.backgroundColor = ._background
        //        self.mo_request_send()
    }
    
    open override func mo_request_send() {
        //        weak var wself = self
        //        self.loadingV.loading()
        //        JoTask()._jrl(.意向单处理时间).get.json_handle { (success, msg, obj, response) in
        //            wself?.loadingV.dismiss()
        //            if success
        //            {
        //                if let dict = obj as? NSMutableDictionary
        //                {
        //                    wself?.update(object: dict)
        //                }
        //            }
        //            wself?.mo_request_alert()
        //            }.run()
        mo_request_load(obj: NSMutableDictionary())
        
    }
    
    
    
    open var render:BaseHTMLPage{
        
        
        let path = Bundle.main.path(forResource: "\(self.templateName)", ofType: "html", inDirectory:"\(self.htmlBundle)/template")
        if (path == nil){
            fatalError("无效网页模版:\(templateName)")
        }
        
        let render = TagsRender(templatePath: path!, model: 0)
        let media_path = Bundle.main.path(forResource: "\(self.htmlBundle)", ofType: nil)
        render.mediaBundlePath = media_path
        render.delegate = self
        
        
        //        let temp = Bundle.path(bundleName: self.htmlBundle, fileName: self.templateName, type: "html")
        //        let render = BaseHTMLPage(templatePath: temp!)
        //        render.mediaBundlePath = Bundle(for: StaticController.self).path(forResource: self.htmlBundle, ofType: "bundle")
        render.js_names = self.js.map{return "js/\($0)"}
        render.css_names = self.css.map{return "css/\($0)"}
        return render
    }
    
    
    
    open override func mo_request_load(obj: Any) {
        self.loadingV.alpha = 0.0
        webView?.load(render: self.htmlRender)
        
    }
    
    open func replace(_ tag: String) -> String {
        return ""
    }
    
    deinit {
        net.cancel()
    }
    
}


